<?php
if (!defined('ABSPATH')) {
	define( 'ABSPATH', dirname(dirname(__FILE__)) . '/' );
}

require_once ABSPATH . 'dr-config.php';
require_once ABSPATH . DRINC . '/dr-base.php';
require_once ABSPATH . DRINC . '/dr-functions.php';

require_once ABSPATH . DRINC . '/dr-smarty.php';

require_once ABSPATH . DRINC . '/dr-page-check-login.php';

if (isset($_POST['submit'])) {
	$us_sitename = isset($_POST['sitename']) ? $_POST['sitename'] : 'iDaren';
	$us_question_per_page = isset($_POST['question_per_page']) ? intval($_POST['question_per_page']) : 10;
	$us_answer_per_page = isset($_POST['answer_per_page']) ? intval($_POST['answer_per_page']) : 10;
	$us_days_to_display = isset($_POST['days_to_display']) ? intval($_POST['days_to_display']) : 30;
	$us_display_day_top=isset($_POST['display_day_top']) ? intval($_POST['display_day_top']) : 1;
	$us_display_week_top=isset($_POST['display_week_top']) ? intval($_POST['display_week_top']) : 1;
	$us_display_month_top=isset($_POST['display_month_top']) ? intval($_POST['display_month_top']) : 1;
	$us_display_all_top=isset($_POST['display_all_top']) ? intval($_POST['display_all_top']) : 1;
	$us_question_top_num=isset($_POST['question_top_num']) ? intval($_POST['question_top_num']) : 5;
	$us_tag_top_num=isset($_POST['tag_top_num']) ? intval($_POST['tag_top_num']) : 5;
	$us_user_top_num=isset($_POST['user_top_num']) ? intval($_POST['user_top_num']) : 5;
	
	
	$options = array();
	$options['sitename'] = $us_sitename;
	$options['question_per_page'] = $us_question_per_page;
	$options['answer_per_page'] = $us_answer_per_page;
	$options['days_to_display'] = $us_days_to_display;
	$options['display_day_top'] = $us_display_day_top;
	$options['display_week_top'] = $us_display_week_top;
	$options['display_month_top'] = $us_display_month_top;
	$options['display_all_top'] = $us_display_all_top;
	$options['question_top_num'] = $us_question_top_num;
	$options['tag_top_num'] = $us_tag_top_num;
	$options['user_top_num'] = $us_user_top_num;
	
	if (dr_update_general_settings($options) === false) {
		die('an error occur when update general settings');
	}
}

$smarty->assign('page_title', '普通设置 - iDaren');

# 获得系统的相关配置信息
$options = dr_get_general_settings();
$smarty->assign('options', $options);

$smarty->display('admin/admin-general-settings.tpl');

# ================================== #
# functions
# ================================== #
/**
 * 获得系统的普通设置
 * 
 * @return array 
 */
function dr_get_general_settings() {
	$posts = array(
		'action' => 'get_general_settings'
	);
	$json_result = dr_server_request('dr-app-options.php', $posts);
	return json_decode($json_result, true);
}

/**
 * 更新普通设置(general settings)
 *
 * @param array $options Data to update (in column => value pairs).
 *
 * @return int|false The number of rows updated, or false on error.
 */
function dr_update_general_settings($options) {
	$posts = array(
		'action' => 'update_general_settings',
		'options' => json_encode($options)
	);
//	$posts = array_merge($posts, $options);

	
	$json_result = dr_server_request('dr-app-options.php', $posts);
	return;
}